//index.js
//获取应用实例
const app = getApp()
var searchContent;
Page({
  data: {
    banner: [],
    nav: [],
    hot: [],
    news: [],
    domain: app.globalData.domain,
    random: '',
    keywords: '',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    this.banner();
    this.nav();
    this.hot();
    this.news();
    this.createNonceStr();
    wx.request({
      url: getApp().globalData.domain + 'api/company_information',
      data: {},
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res.data[0].telephone)
        getApp().globalData.telephone = res.data[0].telephone
      },
      fail: function (err) { },
      complete: function () { }
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }, 
  createNonceStr: function () {
    var random = Math.random().toString().substr(2, 15)
    this.setData({ random: random })
  },
  inputSearch:function(e){
    searchContent=e.detail.value
  },
  /**
   * 获取搜索框的值
   */
  search: function () {
    this.setData({ keywords: searchContent })
    var keywords = this.data.keywords
    if (keywords == null && keywords == undefined){
      wx.showToast({
        title: '搜索为空',
      })
    }
    if (keywords != '' && keywords != undefined) {
      wx.navigateTo({
        url: '../search/search?keywords=' + keywords
      })
    }
  },

  /**
   * 搜索功能
   */
  searchGoods: function () {
    var keywords = this.data.keywords
    if (keywords != '') {
      wx.navigateTo({
        url: '../search/search?keywords=' + keywords
      })
    }
  },
  banner: function () {
    var banner=wx.getStorageSync('banner');
    var that = this;
    if (banner){
      that.setData({
        banner: res.data.data
      })
    }else{
      wx.request({
        url: getApp().globalData.domain + 'api/banner',
        data: {},
        header: {
          "Content-Type": "applciation/json"
        },
        method: "GET",
        success: function (res) {
          // console.log(res.data.data);
          that.setData({
            banner: res.data.data
          })
        },
        fail: function (err) { },
        complete: function () { }
      })
    }
  },
  nav: function () {
    var that = this;
      wx.request({
        url: getApp().globalData.domain + 'api/category',
        data: {},
        header: {
          "Content-Type": "applciation/json"
        },
        method: "GET",
        success: function (res) {
          // console.log(res.data.data);
          that.setData({
            nav: res.data.data
          })
        },
        fail: function (err) { },
        complete: function () { }
      })
  },
  hot: function () {
    var hot = wx.getStorageSync('hot');
    var that = this;
    if (hot) {
      that.setData({
        hot: res.data.data
      })
    } else {
      wx.request({
        url: getApp().globalData.domain + 'api/commodity/hot',
        data: {},
        header: {
          "Content-Type": "applciation/json"
        },
        method: "GET",
        success: function (res) {
          // console.log(res.data.data);
          that.setData({
            hot: res.data.data
          })

        },
        fail: function (err) { },
        complete: function () { }
      })
    }
  },
  news: function () {
    var news = wx.getStorageSync('news');
    var that = this;
    if (news) {
      that.setData({
        news: res.data.data
      })
    } else {
      wx.request({
        url: getApp().globalData.domain + 'api/commodity/latest_data',
        data: {},
        header: {
          "Content-Type": "applciation/json"
        },
        method: "GET",
        success: function (res) {
          // console.log(res.data);
          that.setData({
            news: res.data
          })

        },
        fail: function (err) { },
        complete: function () { }
      })
    }
  },
  switchTab: function (e) {
    // console.log(e)
    app.globalData.globalId = e.currentTarget.dataset.id
    wx.switchTab({
      url: '../category/category'
    })
  },
})
